//
//  YZMBankListViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/18.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMBankListViewController.h"
#import "YZMBankCardService.h"
#import "YZMResultView.h"
#import "YZMSeparatorCell.h"
#import "YZMBankModel.h"

@interface YZMBankListViewController()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *bankTableView;
@property (nonatomic, strong) NSArray *bankList;
@property (nonatomic, strong) YZMResultView *resultView;

@end

@implementation YZMBankListViewController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.title = @"选择银行";
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    [self createObjects];
    [self fetchBankList];
}

#pragma mark -- Network Request

- (void)fetchBankList{
    
    NSArray *bankList = [YZMBankCardService sharedService].bankList;
    if (bankList && bankList.count != 0 ) {
        
        self.bankList = bankList;
        [self.bankTableView reloadData];
        
    }else{
    
        self.resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
        [[YZMBankCardService sharedService] fetchBankListSuccess:^{
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.bankList = [YZMBankCardService sharedService].bankList;
            [self.bankTableView reloadData];
            
        }failure:^(NSError *error){
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
            [error yz_showMessage];
        }];
    }
}

#pragma mark -- UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"BankListUnitCell";
    YZMSeparatorCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YZMSeparatorCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [YZMAppStyle colorForGrayText];
        cell.textLabel.font = [YZMAppStyle mediumSizeFont];
        cell.rightMargin = 0.0;
        [cell.contentView insertSubview:cell.textLabel belowSubview:cell.bottomLineView];
    }
    
    YZMBankModel *instance = [self.bankList objectAtIndex:indexPath.row];
    cell.textLabel.text = instance.name;
    cell.textLabel.textColor = [YZMAppStyle colorForMainTextColor];
    cell.imageView.image = instance.iconImage;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = nil;
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row?indexPath.row:1;
    indexCount.count = self.bankList.count;
    cell.indexCount = indexCount;
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.bankList.count;
}

#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.navigationController popViewControllerAnimated:YES];
    if (self.selectAction) {
        
        self.selectAction(self.bankList[indexPath.row]);
    }
}


#pragma mark -- Private Method

- (void)createObjects{
    
    self.bankTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.bankTableView.dataSource = self;
    self.bankTableView.delegate = self;
    self.bankTableView.backgroundColor = [UIColor clearColor];
    self.bankTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.bankTableView];
    [self.bankTableView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.left.right.equalTo(self.view);
        maker.bottom.equalTo(self.view).offset(-10);
    }];
}

#pragma mark -- Params Manager

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMBankListViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams:params];
    return ctrl;
}

- (void)updateWithParams:(NSDictionary *)params{
    
    self.selectAction = params[@"selectAction"];
}

@end
